/**
 * 
 * ROLL PANE CONTROLLER
 * 
 * @Ning
 * 
 * 2009-2-21
 * 
 * */

package com.components.rollpane
{
	import com.components.button.MCButton;
	
	import core.ease.EaseFadeEvents;
	import core.ease.MotionController;
	import core.section.Controller;
	
	import debug.Log;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	[Event(name="ready_for_display", type="core.ease.EaseFadeEvents")]
	[Event(name="ease_in_over", type="core.ease.EaseFadeEvents")]
	[Event(name="ease_out_over", type="core.ease.EaseFadeEvents")]

	public class RollPane extends Controller
	{
		//START...
		
		
		//PROPERTY...
		
		private var view:RollPaneView;
		
		private var btnLeader:MCButton;
		
		private var _content:MotionController;
		
		private var isRoll:Boolean;
		
		//CONSTRUCTOR...
		public function RollPane(target:DisplayObject)
		{
			super(target);
			
			init();
			
		}
		
		//METHODS...
		
		// initializing...
		private function init():void
		{
			
			view = new RollPaneView(target);
			
			if(view.btnLeader)
			{
				
				btnLeader = new MCButton(view.btnLeader);
				
				btnLeader.target.addEventListener(MouseEvent.MOUSE_OVER,btnLeaderHandler,false,1);
				
				btnLeader.target.addEventListener(MouseEvent.MOUSE_OUT,btnLeaderHandler,false,1);
				
				target.addEventListener(MouseEvent.ROLL_OVER,tarRollOverHandler);
				
				target.addEventListener(MouseEvent.ROLL_OUT,tarRollOutHandler);
				
			}
			
		}
		
		// on tar roll over...
		private function tarRollOverHandler(evt:MouseEvent):void
		{
			
			isRoll = true;
			
			show();
						
			btnLeader.target.dispatchEvent( new MouseEvent(MouseEvent.MOUSE_OVER));		
		
		}
		
		// on tar roll out...
		private function tarRollOutHandler(evt:MouseEvent):void
		{
		
			isRoll = true;
			
			hide();
		
			btnLeader.target.dispatchEvent( new MouseEvent(MouseEvent.MOUSE_OUT));		
		
		}
		
		
		// on btnLeader dispatch mouse over / mouse out event...
		private function btnLeaderHandler(evt:MouseEvent):void
		{
			
			if(!isRoll) evt.stopImmediatePropagation();		
			
			isRoll = false;
				
		}
		
		// config content evnet listeners...
		private function configContentEventListeners(content:MotionController):void
		{
			
			content.addEventListener(EaseFadeEvents.EASE_IN_OVER,contentEaseInOverHandler);
			
			content.addEventListener(EaseFadeEvents.EASE_OUT_OVER,contentEaseOutOverHandler);
			
			content.addEventListener(EaseFadeEvents.READY_FOR_DISPLAY,contentDisplayHandler);
			
		}
		
		// on content display...
		private function contentDisplayHandler(evt:EaseFadeEvents):void
		{
			
			dispatchEvent(evt);
			
		}
		
		// content ease in over...
		private function contentEaseInOverHandler(evt:EaseFadeEvents):void
		{
			
			var content:MotionController = evt.currentTarget as MotionController;
			
			content.display();
			
		}
		
		
		//content ease out over...
		private function contentEaseOutOverHandler(evt:EaseFadeEvents):void
		{
			
			view.hide();
			
			dispatchEvent(evt);
			
		}
		
		// show content...
		public function show():void
		{
			
			content.easeIn();
			
			if(btnLeader.state != MCButton.OVER_STATE) btnLeader.state = MCButton.OVER_STATE;
			
			view.show();
			
		}
		
		
		// hide content...
		public function hide():void
		{
			
			if(btnLeader.state != MCButton.OUT_STATE) btnLeader.state = MCButton.OUT_STATE;
			
			content.easeOut();
			
		}
		
		
		// set roll pane content...
		public function set content(_content:MotionController):void
		{
			
			this._content = _content;
			
			view.addContent(_content.target);
			
			configContentEventListeners(_content);
					
		}
		
		// get roll pane content...
		public function get content():MotionController
		{
		
			return _content;
		
		}
		
		
		
		
		
		//END...
	}
}